package br.com.localfloripa.util;

import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;

import br.com.localfloripa.entities.Email;
import br.com.localfloripa.entities.FormatoEmail;


public class MailUtils {
	
	public static final String SMTP_FROM = "contato@localfloripa.com.br";
	public static final String SMTP_HOST = "smtp.googlemail.com";
	public static final Integer SMTP_PORT = 465;

	
	public MailUtils() {
		
	}
	
	public static void sendMail(Email email,FormatoEmail formato) throws Exception {
		
		if (formato.equals(FormatoEmail.HTML))
			sendMailHtml(email);
		else
			sendMailText(email);
	}
	
	private static void sendMailHtml(Email msg) throws Exception {
		
		ResourceBundle bundle = ResourceBundle.getBundle("messages.MessageBundle");
		
		HtmlEmail email = new HtmlEmail();
		email.setHostName(SMTP_HOST);
		email.setSmtpPort(SMTP_PORT);
		email.setTLS(true);
		email.setSSL(true);
		email.setAuthenticator(new DefaultAuthenticator(bundle.getString("smtp.user"), bundle.getString("smtp.pass")));
		for (Map.Entry<String, String>  entry :msg.getDestinatarios().entrySet()) {			
			email.addTo(entry.getValue());			
		}
		email.setFrom(SMTP_FROM);
		email.setSubject(msg.getTitulo());
		email.setHtmlMsg(msg.getCorpo());
		email.setTextMsg(bundle.getString("message.email.html.support"));
		
		email.send();
	}
	
	private static void sendMailText(Email msg) throws Exception {
		
		ResourceBundle bundle = ResourceBundle.getBundle("messages.MessageBundle");
		
		SimpleEmail email = new SimpleEmail();
		email.setHostName(SMTP_HOST);
		email.setSmtpPort(SMTP_PORT);
		email.setTLS(true);
		email.setSSL(true);
		email.setAuthenticator(new DefaultAuthenticator(bundle.getString("smtp.user"), bundle.getString("smtp.pass")));
		for (Map.Entry<String, String>  entry :msg.getDestinatarios().entrySet()) {			
			email.addTo(entry.getValue());			
		}
		email.setFrom(SMTP_FROM);
		email.setSubject(msg.getTitulo());
		email.send();
	}
	
	
	
	

}
